package com.ershui.blog.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    @Autowired
    private FilePathConfig filePathConfig;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //建立本地存储路径和虚拟路径映射关系
        registry.addResourceHandler(filePathConfig.getFileOrmPath()+"/**").addResourceLocations("file:"+filePathConfig.getFileOriPath()+"/");
        //因为自定义实现了WebMvcConfigurationSupport，springboot默认实现的静态资源文件映射关系需要加上去，不然静态资源文件访问不到
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        super.addResourceHandlers(registry);
    }
}
